package com.example.taobaou.ui.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.taobaou.R;
import com.example.taobaou.model.domain.HomePagerContent;
import com.example.taobaou.model.domain.IBsaeInfo;
import com.example.taobaou.model.domain.ILinearItemInfo;
import com.example.taobaou.utils.UrlUtils;

import java.util.ArrayList;
import java.util.List;

public class HomePagerContentAdapter extends RecyclerView.Adapter<HomePagerContentAdapter.InnerHolder> {

    private List<ILinearItemInfo> data = new ArrayList<>();
    private View itemView;
    private OnListItemClickListener mItemClickListener = null;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        Log.d(String.valueOf(HomePagerContentAdapter.class), "onCreateViewHolder: ------------------------------");
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_pager_content, parent, false);
        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
//        Log.d(String.valueOf(HomePagerContentAdapter.class), "onBinViewHolder: ------------------------------" + position);
       ILinearItemInfo dataBean = data.get(position);
        //设置数据
        holder.setData(dataBean);
        //设置点击监听
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(dataBean);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void loadData(List<?extends ILinearItemInfo> contents) {
        data.clear();
        data.addAll(contents);
        notifyDataSetChanged();
    }

    public void addData(List<?extends ILinearItemInfo> contents) {
        //拿到更新前的size
        int oldSize = data.size();
        //加载更多数据
        data.addAll(contents);
        //更新item数据，该方法为从第几条到第几条
        notifyItemRangeChanged(oldSize, contents.size());
    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        public ImageView cover;
        public TextView title;
        public TextView offPriseTv;//券价
        public TextView finalPriseTv;//券后价
        public TextView originalPriseTv;//原价
        public TextView sellCountTv;//销售量

        private long coupon_amount;
        private Context context;


        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.goods_title);
            cover = itemView.findViewById(R.id.goods_cover);
            offPriseTv = itemView.findViewById(R.id.goods_off_prise);
            finalPriseTv = itemView.findViewById(R.id.final_pirse);
            originalPriseTv = itemView.findViewById(R.id.goods_orginal_prise);
            sellCountTv = itemView.findViewById(R.id.goods_sell_count);


        }


        public void setData(ILinearItemInfo dataBean) {
            coupon_amount = dataBean.getCouponAmount();
            context = itemView.getContext();
            //设置标题
            title.setText(dataBean.getTitle());
            //设置图片
            //要先获得控件的宽高
            ViewGroup.LayoutParams layoutParams = cover.getLayoutParams();
            int width = layoutParams.width;
            int height = layoutParams.height;
            int coverSize = (width > height ? width : height) / 2;

            Glide.with(context).load(UrlUtils.getTicket(dataBean.getCover(),coverSize)).into(cover);
            Log.d(String.valueOf(HomePagerContentAdapter.class), "url------->"+dataBean.getCover());
            //设置优惠券价格
            offPriseTv.setText(String.format(context.getString(R.string.tex_prise), coupon_amount));
            //设置原价
            String final_price = dataBean.getFinalPrise();
            originalPriseTv.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);//设置划掉效果
            originalPriseTv.setText(String.format(context.getString(R.string.tex_orginal), final_price));
            //设置券后价
            Float resultPrise = Float.parseFloat(final_price) - coupon_amount;
            finalPriseTv.setText(String.format("%.2f", resultPrise));
            //设置销售量
            sellCountTv.setText(dataBean.getVolume() + "");
            // Log.d(String.valueOf(HomePagerContent.class), "Volume------> "+dataBean.getVolume());


        }
    }

    public void setOnListItemClickListener(OnListItemClickListener listener){
        this.mItemClickListener = listener;
    }

    public interface OnListItemClickListener{
        void onItemClick(IBsaeInfo item);
    }
}
